import React from 'react';
import { Col, Icon } from 'antd';
import _ from 'lodash';
import { NumberView, InputEditor } from '@/components/BaseTable';
import { connect } from 'dva';

@connect(state => ({}))
export default class NumCellRenderer extends React.PureComponent {
  state = {};

  changeRow = value => {
    let rowData = _.cloneDeep(this.props.rowData);
    let maxNum = 0;
    if (rowData.isParkingLeaf) {
      maxNum =
        rowData.left_num_parking && rowData.left_num_parking > 0 ? rowData.left_num_parking : 0;
      maxNum += rowData.num_parking;
    }
    if (rowData.isHouseLeaf) {
      maxNum = rowData.left_num_house && rowData.left_num_house > 0 ? rowData.left_num_house : 0;
      maxNum += rowData.num_house;
    }
    let result = value;
    if (value > maxNum) {
      if (rowData.isParkingLeaf) {
        result = maxNum;
      }
      if (rowData.isHouseLeaf) {
        result = maxNum;
      }
    }
    if (rowData.isParkingLeaf) {
      rowData = {
        ...rowData,
        num_parking: result,
      };
    }
    if (rowData.isHouseLeaf) {
      rowData = {
        ...rowData,
        num_house: result,
      };
    }
    console.log(rowData, 'rowData');
    const { dispatch } = this.props;
    dispatch({
      type: 'PriceManageItems/saveRows',
      payload: { rowData, category: this.props.category, isMarketing: this.props.isMarketing },
    });
  };

  render() {
    const { editing, rowData, cellData, batchAdjust } = this.props;
    let maxNum = 0;
    let minNum = 0;
    if (rowData.isParkingLeaf) {
      minNum = 0;
      maxNum =
        rowData.left_num_parking && rowData.left_num_parking > 0 ? rowData.left_num_parking : 0;
      maxNum += rowData.num_parking;
    }
    if (rowData.isHouseLeaf) {
      minNum = 1;
      maxNum = rowData.left_num_house && rowData.left_num_house > 0 ? rowData.left_num_house : 0;
      maxNum += rowData.num_house;
    }
    return (
      <Col
        onClick={() => {
          console.log(cellData);
        }}
      >
        {editing && rowData.canEditOther && batchAdjust * 1 === 0 ? (
          <InputEditor
            validateStatus={rowData.errorNum ? 'error' : 'success'}
            onChange={this.changeRow}
            // max={maxNum}
            // min={minNum}
            precision={0}
            {...this.props}
          />
        ) : (
          <NumberView
            valueStyle={
              editing && (rowData.errorSplitNum || rowData.errorNum) ? { color: '#ff4d4f' } : {}
            }
            prefix={
              editing && (rowData.errorSplitNum || rowData.errorNum) ? (
                <Icon type="info-circle" theme="filled" />
              ) : (
                ''
              )
            }
            precision={0}
            {...this.props}
          />
        )}
      </Col>
    );
  }
}
